package com.demo.thread.semaphore;

import java.util.concurrent.Semaphore;

/**
 * @author : YU.TAN
 * @date : 2021/11/9 17:56
 */
public class SemaphoreTest1 {



    public static void main(String[] args) {
        Service1 service1=new Service1();

        new Thread(service1).start();
        new Thread(service1).start();
        new Thread(service1).start();
        new Thread(service1).start();
        new Thread(service1).start();
        new Thread(service1).start();
        new Thread(service1).start();
    }



}

class Service1 implements Runnable{
    private static Semaphore semaphore=new Semaphore(1);

    public void testMethod(){

        try {
            semaphore.acquire();
            System.out.println(Thread.currentThread().getName()+"-begin timer:"+System.currentTimeMillis());

            Thread.sleep(100);
            System.out.println(Thread.currentThread().getName()+"-end timer:"+System.currentTimeMillis());


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        testMethod();
    }
}
